<?php
namespace Admin\Model;

use Think\Model;
/**
 * 岗位管理模型
 *
 */
class RoleModel extends Model{
    const ROOT_ROLE_ID = 1;

     public function sub_role( $role_id, $include_me = true ){
        $wheres = array();
        $wheres['_string'] = '1=1';
        if( !$include_me )
            $wheres['role_id '] = array('neq', $role_id );
        $wheres['is_deleted '] = array('neq', 1);
        $seclist = $this->field("role_id,parent_role")->where( $wheres )->select();
        $treelist = list_to_tree($seclist, 'role_id', 'parent_role','_child', $sector_id );
        $arrlist = tree_to_list( $treelist, '_child');
        $seclist = array();
        foreach( $arrlist as $vc ){
            $seclist[] = $vc['role_id'];
        }
        if( $include_me )
            $seclist[] = $role_id;

        $treelist = null;
        $arrlist = null;

        return $seclist;
    }

    public function add_role( $rights ){
        $r = array('r'=> 0,'d'=>NULL,'i'=>'Server Busy!' );
        if( !$this->parent_role || !$this->role_name || !$this->sector_id ){
            $r['i'] = '参数提交错误';
        }
        else if( !$rights || (is_array($rights) && count($rights)==0  ) ){
            $r['i'] = '创建的岗位要绑定权限';
        }
        else{
            $r['d'] = $this->add();
            if( $r['d'] > 0 ){
                $dr = array();
                $dr['create_time'] = date('Y-m-d H:i:s');
                $dr['role_id'] = $r['d'];
                $MRoleRight = M('RoleRights');
                foreach( $rights as $vr ){
                    $dr['right_id'] = $vr;
                    $MRoleRight->data( $dr )->add();
                }
                $r['r'] = 1;
                $r['i'] = 'OK';
            }
            else{
                $r['i'] = '岗位添加失败';
            }
        }

        return $r;
    }

    public function edit_role( $rights ){
        $r = array('r'=> 0,'d'=>NULL,'i'=>'Server Busy!' );
        if( !$this->parent_role || !$this->role_name || !$this->sector_id ){
            $r['i'] = '参数提交错误';
        }
        else if( !$this->role_id ){
            $r['i'] = '编辑对象不存在';
        }
        else if( !$rights || (is_array($rights) && count($rights)==0  ) ){
            $r['i'] = '创建的岗位要绑定权限';
        }
        else{
            $Mdo = M("Role");
            $role_id = $this->role_id;
            $wherec = array();
            $wherec['role_id'] = array('eq', $role_id );
            $drole = $Mdo->where( $wherec )->find();
            /*更新上下级关联关系*/
            if( $drole['parent_role'] != $this->parent_role && self::ROOT_ROLE_ID!= $role_id){
                $whereup = array();
                $whereup['parent_role'] = array('eq', $role_id);
                $dup = array();
                $dup['parent_role'] = $drole['parent_role'];
                $Mdo->where( $whereup )->save( $dup );
            }

            $r['d'] = $this->save();
            $MRoleRight = M('RoleRights');
            $dr = array();
            $dr['create_time'] = date('Y-m-d H:i:s');
            $dr['role_id'] = $role_id;

            $MRoleRight->where( $wherec )->delete();

            $r['f'] = $MRoleRight->getLastSql();
            foreach( $rights as $vr ){
                $dr['right_id'] = $vr;
                $MRoleRight->data( $dr )->add();
            }
            $r['r'] = 1;
            $r['i'] = 'OK';
        }

        return $r;
    }

}